/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testdesign;

// Java Libraries
import java.util.LinkedHashMap;
// Atof Libraries.
import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.model.testdesign.ConditionsModel;
import org.testis.gui.mvc.view.testdesign.ConditionsView;
import org.testis.orm.conditionhandler.ConditionHandlerFactory;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.testis.orm.refinedclasses.TestDesignTreeNode;


/**
 *
 * @author Administrator
 */
public class ConditionsController extends AbstractController {

    public static final String CHANGE_CONDITIONS_LIST = "ConditionsList";
    private ConditionsView m_condView;
    private ConditionsModel m_condCont;
    private static ConditionsController m_SingleController;
    private boolean m_editmode = false;

    private ConditionsController(boolean editmode) {
        super();
        m_editmode = editmode;
    }

    public static ConditionsController createInstance(boolean editmode) {
        if (m_SingleController == null) {
            m_SingleController = new ConditionsController(editmode);
        }
        return m_SingleController;
    }

    public static ConditionsController getInstance() {
        return m_SingleController;
    }

    public void operationClose() {
        cleanAll();
        m_SingleController = null;
    }

    public void initialize() {

        m_condCont = new ConditionsModel();
        addModel(m_condCont);
        m_condView = new ConditionsView();
        addView(m_condView);


        LinkedHashMap<Integer, String> conditionsList = new LinkedHashMap<Integer, String>();

        conditionsList = (LinkedHashMap<Integer, String>) getConditionsListFromDB().clone();

        operationSetConditionsList(conditionsList);
    }
    //operations

    public void operationSetConditionsList(LinkedHashMap<Integer, String> condList) {
        setModelProperty(CHANGE_CONDITIONS_LIST, condList);
    }

    public LinkedHashMap<Integer, String> operationGetConditionsList() {
        return (LinkedHashMap<Integer, String>) getModelProperty(CHANGE_CONDITIONS_LIST);
    }

    public void operationRemoveItemFromList(Integer itemId) {

        LinkedHashMap<Integer, String> ourModelList =
                (LinkedHashMap<Integer, String>) operationGetConditionsList().clone();
        ourModelList.remove(itemId);
        operationSetConditionsList(ourModelList);
    }

    public void operationEditItemFromList(Integer condId, String condText) {
        ConditionTextEditorController.createInstance().initialize(condId, condText);
        ConditionTextEditorController.createInstance().getConditioTextEditorView().setVisible(true);
    }

    public void operationNewItemFromList() {
        LinkedHashMap<Integer, String> existingMap = (LinkedHashMap<Integer, String>) getModelProperty(CHANGE_CONDITIONS_LIST);
        Integer newIndex = -1;
        while (existingMap.containsKey(newIndex)) {
            newIndex--;
        }

        ConditionTextEditorController.createInstance().initialize(newIndex, null);
        ConditionTextEditorController.createInstance().getConditioTextEditorView().setVisible(true);
    }

    public void operationAddItemToList(Integer condId, String conString) {
        LinkedHashMap<Integer, String> existingList = new LinkedHashMap<Integer, String>();
        existingList = (LinkedHashMap<Integer, String>) ((LinkedHashMap<Integer, String>) getModelProperty(CHANGE_CONDITIONS_LIST)).clone();

        existingList.put(condId, conString);
        operationSetConditionsList(existingList);

    }   

    /**
     * This method is used to get the Conditions List from Database.
     * @param testDesignTreeNode TreeNodeId to get the conditions for related Node
     * @return HashMap<Integer,String> conditionsList from DB as HashMap
     */
    private LinkedHashMap<Integer, String> getConditionsListFromDB(/*later write node Id as parameter*/) {
        
        LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
        if (m_editmode) {// poll from database only in edit mode
            // no need to poll when creating new node.
            TestDesignTreeNode node = TestDesignControllerList.newInstance().
                    getTreeController().getSelectedTreeNode();
            LinkedHashMap<Integer, String> hashmap =
                    ConditionHandlerFactory.getInstance(node).getConditions(node);

            map.putAll(hashmap);
        }
        return map;
    }
    // Getters

    public ConditionsView getConditionView() {
        return m_condView;
    }

    public LinkedHashMap<Integer,String> getConditionsList() {
        LinkedHashMap<Integer,String> retCondList = new LinkedHashMap<Integer,String>();
        LinkedHashMap<Integer, String> listOfConditions =
                (LinkedHashMap<Integer, String>) getModelProperty(
                CHANGE_CONDITIONS_LIST);

        retCondList = (LinkedHashMap<Integer, String>)listOfConditions.clone();
        
        return retCondList;

    }
    // Setters
}
